package cn.mw.cmdb.entity;

import cn.mw.cmdb.param.ImportContextParam;
import cn.mw.components.mongodb.entity.CriteriaOpsType;
import cn.mw.components.mongodb.entity.MPropertyTypeList;
import cn.mw.microMonitorCommon.entity.cmdb.ModelExportDataInfoParam;
import cn.mw.microMonitorCommon.entity.cmdb.MwImportResultParam;
import com.google.common.base.Strings;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PropertyTypeList extends PropertyType<PropertyTypeList, List> {
    public static final String ID = MPropertyTypeList.ID;

    private MPropertyTypeList mPropertyTypeList = new MPropertyTypeList();

    public PropertyTypeList() {
        setId(ID);
        setName("数组");
        setValueTip("List");
    }

    @Override
    public Object tableHeaderTipInfo(PropertyTypeRef propertyTypeRef) {
        return "多个数据用“,”隔开";
    }

    @Override
    public Criteria doCriteria(CriteriaOpsType opsType, String name, Object value) {
        return mPropertyTypeList.doCriteria(opsType, name, value);
    }

    @Override
    public CriteriaOpsType getDefaultCriteriaOpsType() {
        return CriteriaOpsType.in;
    }

    @Override
    public PropertyTypeList newObj() {
        return new PropertyTypeList();
    }


    @Override
    public void refresh() {

    }

    @Override
    public Object convertValue(Object value) {
        if (value != null && value instanceof List) {
            List infos = new ArrayList<>();
            List listInfo = (List) value;
            for (Object str : listInfo) {
                if (str != null) {
                    if (str instanceof String) {
                        if (!Strings.isNullOrEmpty(strValueConvert(str).trim())) {
                            infos.add(strValueConvert(str).trim());
                        }
                    } else {
                        infos.add(str);
                    }
                }
            }
            return infos;
        }
        return null;
    }

    @Override
    public Object getDataValue(ImportContextParam contextParam) {
        MwImportResultParam resultParam = new MwImportResultParam();
        String message = "";
        ModelExportDataInfoParam importParam = contextParam.getParam();
        //属性名称
        String tableName = importParam.getTableName();
        Object cellValue = importParam.getCellValue();
        Map<String, Object> matchMap = new HashMap<>();
        importCheckInfo(resultParam, contextParam, matchMap);
        String data = strValueConvert(cellValue);
        if (StringUtils.hasText(data)) {
            String[] split = data.split(",");
            List<String> infos = new ArrayList<>();
            if (split != null) {
                for (String str : split) {
                    if (!Strings.isNullOrEmpty(str.trim())) {
                        infos.add(str.trim());
                    }
                }
            }
            resultParam.setCellValue(infos);
        }
        return resultParam;
    }
}
